package cn.itcast.beanfactory.utils;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;

import cn.itcast.beanfactory.BeanFactory;
import cn.itcast.beanfactory.cfg.BeanConfig;
import cn.itcast.beanfactory.cfg.PropertyConfig;
import cn.itcast.xml.utils.XmlUtils;

public class BeanFactoryUtils {
	public static void load(BeanFactory factory, String xmlName) {
		Document doc = XmlUtils.getDocument(xmlName);
		List<Element> beanEleList = doc.selectNodes("//bean");

		System.out.println("beanEleList.size()="+beanEleList.size());

		for(Element beanEle : beanEleList) {
			System.out.println("beanEle--"+beanEle);
			BeanConfig bc = XmlUtils.toBean(beanEle, BeanConfig.class);
			List<Element> propEleList = beanEle.elements();
			// 把所有的PropertyConfig添加到BeanConfig中
			for(Element propEle : propEleList) {// 遍历<bean>中每个<property>元素
				System.out.println("propEle---"+propEle);
				PropertyConfig pc = XmlUtils.toBean(propEle, PropertyConfig.class);
				System.out.println("pc--"+pc);
				bc.addPropertyConfig(pc);
			}
			System.out.println("bc------22222-------"+bc);
			factory.addBeanConfig(bc);
		}
	}
}




















